package com.github.m6d21.sso.support.util;

import com.github.m6d21.sso.common.util.DateUtil;
import com.github.m6d21.sso.common.util.TraceUtil;
import com.github.m6d21.sso.support.enums.StatusCode;
import com.github.m6d21.sso.support.model.Response;

/**
 *<p>
 *ResponseUtil
 *</p>
 *
 * @author liuyang
 * @date 2018-12-06 Thu 00:13:26
 * @since 1.0.0
 */
public class ResponseUtil {

    private ResponseUtil() {
    }

    public static <T> Response<T> success() {
        return new Response<T>()
            .success(true)
            .code(StatusCode.SUCCESS.getCode())
            .timestamp(DateUtil.getTimestamp())
            .msg(StatusCode.SUCCESS.getMsg())
            .traceId(TraceUtil.getTraceId());
    }

    public static <T> Response<T> success(T data) {
        return new Response<T>()
            .success(true)
            .code(StatusCode.SUCCESS.getCode())
            .timestamp(DateUtil.getTimestamp())
            .data(data)
            .msg(StatusCode.SUCCESS.getMsg())
            .traceId(TraceUtil.getTraceId());
    }

    public static <T> Response<T> fail() {
        return new Response<T>()
            .success(false)
            .code(StatusCode.FAIL.getCode())
            .timestamp(DateUtil.getTimestamp())
            .msg(StatusCode.FAIL.getMsg())
            .traceId(TraceUtil.getTraceId());
    }

    public static <T> Response<T> fail(Integer errorCode, String errorMsg) {
        return new Response<T>()
            .success(false)
            .code(errorCode)
            .timestamp(DateUtil.getTimestamp())
            .msg(errorMsg)
            .traceId(TraceUtil.getTraceId());
    }

    public static <T> Response<T> fail(Integer errorCode, String errorMsg,T data) {
        return new Response<T>()
            .success(false)
            .code(errorCode)
            .timestamp(DateUtil.getTimestamp())
            .data(data)
            .msg(errorMsg)
            .traceId(TraceUtil.getTraceId());
    }
}
